<!--测试页面，项目完成后删除-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>淘淘商城-首页</title>
    <!--bootstrap-->
    <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="../js/jquery.min.js"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="../bootstrap/js/bootstrap.min.js"></script>
    <script src="../js/HttpUtil.js"></script>
    <script src="../js/CacheUtil.js"></script>
</head>
<body>
<nav class="navbar navbar-default">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <a class="navbar-brand" href="#">淘淘商城</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">女装 <span class="sr-only">(current)</span></a></li>
                <li><a href="#">帮助</a></li>
                <li><a href="#">联系我们</a></li>
            </ul>
            <form class="navbar-form navbar-left">
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="Search">
                </div>
                <button type="submit" class="btn btn-default">搜索</button>
            </form>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">购物车</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                       aria-expanded="false">
                        <label id="label_user_name">
                            未登录
                        </label>
                        <span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu">
                        <li><a href="#">个人信息</a></li>
                        <li><a href="#">地址管理</a></li>
                        <li><a href="#">退出</a></li>
                    </ul>
                </li>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>
<div class="container">
    <div class="row">
        <div class="jumbotron">
            <h1>欢迎来到淘淘商城!</h1>
            <p>这是个神奇的网站！</p>
            <p><a class="btn btn-primary btn-lg" href="#" role="button">购物</a></p>
        </div>
    </div>
    <div class="row" id="div_goods_list">
    </div>

</div>

<template id="template_goods">
    <div class="col-xs-2">
        <div class="thumbnail">
            <input type="hidden" value="{id}">
            <img src="img/1.jpg" alt="...">
            <div class="caption">
                <h3>{name}</h3>
                <p>{remark}</p>
                <p>{price}</p>
                <p>商品ID:{id}</p>
                <p>
                    <a href="#" class="btn btn-primary btn-sm" role="button">购物车</a>
                    <a href="#" class="btn btn-danger btn-sm" role="button">立即购买</a>
                </p>
            </div>
        </div>
    </div>
</template>

<script>
    //加载页面->显示商品列表
    $(function () {
        loadUserInfo();
        loadGoodsList();
    });

    function loadUserInfo() {
        let user = getLoginUser();
        $("#label_user_name").html(user.userName);
    }

    //ajax 请求后台获取商品列表数据
    function loadGoodsList() {
        console.log("loadGoodsList");
        //发起ajax请求
        let url = "/goods/listAll?userId=1000&";
        // let params = {userId: 1000, userName: "jerry"};
        httpGetNoParams(url, function (data) {
            //业务处理
            renderingGoodsList(data);
        });
    }

    //渲染显示商品列表
    function renderingGoodsList(goodsList) {
        let templateGoodsHtml = $("#template_goods").html();
        let goodsListHtml = "";
        let tempGoodsHtml;
        //遍历商品列表
        $.each(goodsList, function (index, goods) {
            console.log(goods);
            //后台数据填充到模板中
            tempGoodsHtml = templateGoodsHtml.replace(/{name}/g, goods.name)
                .replace(/{remark}/g, goods.remark)
                .replace(/{id}/g, goods.id)
                .replace(/{price}/g, goods.price);
            console.log(tempGoodsHtml);
            goodsListHtml += tempGoodsHtml;
        });
        //拼接商品展示html
        //html 填充到页面当中
        $("#div_goods_list").html(goodsListHtml);
    }

</script>


</body>
</html>